package impl

import (
	"adam2/internal/model"
)

type Real9QuantStockFilterDaoImpl struct {
	*BaseDaoImpl
}

// 返回dao实现类
func GetReal9QuantStockFilterDaoImpl() *Real9QuantStockFilterDaoImpl {
	return &Real9QuantStockFilterDaoImpl{GetBaseDaoImpl()}
}

// 返回表名
func (r *Real9QuantStockFilterDaoImpl) FindTableName() string {
	return "REAL9_Q_STOCK_FILTER"
}

func (r *Real9QuantStockFilterDaoImpl) TruncateTable() {
	r.db.Exec("truncate table real9_q_stock_filter")
}

func (r *Real9QuantStockFilterDaoImpl) InsertIntoReal9QuantStockFilterFromStockTransactionData(transactionDate string) {
	r.db.Exec("insert /*+ append */ into real9_q_stock_filter(stock_code, total_market_value) "+
		"select distinct stda.code_, stda.total_market_value "+
		"from stock_transaction_data_all stda "+
		"where stda.date_=to_date(?, 'yyyy-mm-dd') order by stda.total_market_value asc", transactionDate)
}

// 删除总市值不满足要求的股票
func (r *Real9QuantStockFilterDaoImpl) DeleteByTotalMarketValueBottomLimit(statistics string, marketValueMainBoardBottomLimit float64, marketValueCyBBoardBottomLimit float64) {
	r.db.Exec("delete real9_q_stock_filter t2 where t2.stock_code in("+
		"select distinct t.code_ from stock_transaction_data_all t where t.date_ = to_date(?, 'yyyy-mm-dd') "+
		"and t2.stock_code=t.code_ and t.total_market_value/100000000<? "+
		"and (t.code_ like '60%' or t.code_ like '00%'))",
		statistics, marketValueMainBoardBottomLimit)
	r.db.Exec("delete real9_q_stock_filter t2 where t2.stock_code in("+
		"select distinct t.code_ from stock_transaction_data_all t where t.date_ = to_date(?, 'yyyy-mm-dd') "+
		"and t2.stock_code=t.code_ and t.total_market_value/100000000<? "+
		"and t.code_ like '30%' or t.code_ like '68%')",
		statistics, marketValueCyBBoardBottomLimit)
}

// 只保留总市值最小的(10*账号数量+4)只股票
func (r *Real9QuantStockFilterDaoImpl) RetainMinTotalMarketValue(maxHoldStockNumber int) {
	r.db.Exec("delete real9_q_stock_filter t2 where t2.stock_code not in("+
		"select t1.stock_code from (select * from real9_q_stock_filter t "+
		"order by t.total_market_value asc) t1 "+
		"where rownum<=?*("+
		"select count(*) from real9_q_account)+4)", maxHoldStockNumber)
}

// 查询全部记录
func (r *Real9QuantStockFilterDaoImpl) FindAll() model.Real9QuantStockFilterArray {
	var real9QuantStockFilterArray model.Real9QuantStockFilterArray
	r.db.Raw("select * from real9_q_stock_filter").Scan(&real9QuantStockFilterArray)
	return real9QuantStockFilterArray
}

// 根据code删除记录
func (r *Real9QuantStockFilterDaoImpl) DeleteByStockCode(stockCode string) {
	r.db.Exec("delete from real9_q_stock_filter t where t.stock_code = ?", stockCode)
}

// 删除开盘价不满足要求的股票
func (r *Real9QuantStockFilterDaoImpl) DeleteOpenPriceBottomLimit(transactionDate string, openPriceBottomLimit float64) {
	r.db.Exec("delete real9_q_stock_filter t2 where t2.stock_code in("+
		"select t.code_ from stock_transaction_data_all t where t.date_ = to_date(?, 'yyyy-mm-dd') "+
		"and t2.stock_code=t.code_ and t.open_price<?)", transactionDate, openPriceBottomLimit)
}

// 查询real9_q_stock_filter表的记录，不包括real9_q_stock_transact_record表中还没有卖出的记录，不包括当天是最后一个交易日的股票
func (r *Real9QuantStockFilterDaoImpl) FindByStockCodeNotInAndReal9QuantStockTransactionRecordAndDateNull(transactionDate string) model.Real9QuantStockFilterArray {
	var real9QuantStockFilterArray model.Real9QuantStockFilterArray
	r.db.Raw("select t.* from real9_q_stock_filter t where t.stock_code not in("+
		"select t1.stock_code from real9_q_stock_transact_record t1 "+
		"where t1.sell_date is null and t1.sell_price is null and t1.sell_amount is null) "+
		"and t.stock_code not in("+
		"select si_.code_ from stock_info si_ where si_.last_transaction_date=to_date(?,'yyyy-mm-dd')) "+
		"order by t.total_market_value asc, t.stock_code", transactionDate).Scan(&real9QuantStockFilterArray)
	return real9QuantStockFilterArray
}

// 查询记录总数
func (r *Real9QuantStockFilterDaoImpl) Count() int {
	var quantStockFilterNumber int = -1
	r.db.Raw("select count(*) from real9_q_stock_filter").Scan(&quantStockFilterNumber)
	return quantStockFilterNumber
}

// 插入记录
func (r *Real9QuantStockFilterDaoImpl) Insert(accountName string, stockCode string, date string, openPrice float64, buyOrSellAmount int, registrationFee float64, commission float64) {
	r.db.Exec("insert into real9_q_stock_transact_record(ACCOUNT_NAME, STOCK_CODE, BUY_DATE, BUY_PRICE, BUY_AMOUNT, "+
		"REGISTRATION_FEE_WHEN_BUY, COMMISSION_WHEN_BUY) "+
		"values(?, ?, to_date(?, 'yyyy-mm-dd'), ?, ?, ?, ?)",
		accountName, stockCode, date, openPrice, buyOrSellAmount, registrationFee, commission)
}
